package com.itheima.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.PmsProductAttribute;
import com.itheima.domain.PmsProductAttributeCategory;
import com.itheima.service.db.PmsProductAttributeCategoryService;
import com.itheima.service.db.PmsProductAttributeService;
import com.itheima.util.R;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.PmsProductAttributeCategoryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class PmsProductAttributeCategoryManager {

    @DubboReference
    PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    @DubboReference
    PmsProductAttributeService pmsProductAttributeService;

    /**
     * 添加商品属性分类
     * @param name
     * @return
     */
    public R addProductAttributeCategory(String name) {
        Boolean res = pmsProductAttributeCategoryService.addProductAttributeCategory(name);
        return res?R.success("添加商品属性分类成功"):R.error(500,"添加失败,属性名重复");
    }

    /**
     * 删除单个商品属性分类
     * @param id
     * @return
     */
    public R deleteOne(Long id) {
        pmsProductAttributeCategoryService.deleteOne(id);
        return R.success("删除成功");
    }

    /**
     * 分页获取所有商品属性分类
     * @param pageNum
     * @param pageSize
     * @return
     */
    public R findByPage(Integer pageNum, Integer pageSize) {
        PageBeanVo vo = pmsProductAttributeCategoryService.findByPage(pageNum,pageSize);
        return R.success(vo);
    }

    /**
     * 获取所有商品属性分类及其下属性
     * @return
     */
    public R findWithAttr() {
        //1.获取所有商品属性分类
        List<PmsProductAttributeCategory> ppacList = pmsProductAttributeCategoryService.findWithAttr();
        List<PmsProductAttributeCategoryVo> voList = new ArrayList<>();
        //2.遍历商品属性分类
        if (CollUtil.isNotEmpty(ppacList)) {
            for (PmsProductAttributeCategory pmsProductAttributeCategory : ppacList) {
                PmsProductAttributeCategoryVo vo = new PmsProductAttributeCategoryVo();
                //3.根据id去属性表中查询属性
                List<PmsProductAttribute> productAttributeList = pmsProductAttributeService.findByCategoryId(pmsProductAttributeCategory.getId());
                //4.拷贝数据
                BeanUtil.copyProperties(pmsProductAttributeCategory,vo);
                vo.setProductAttributeList(productAttributeList);
                voList.add(vo);
            }
        }
        //5.返回响应结果
        return R.success(voList);
    }

    /**
     * 修改商品属性分类
     * @param id
     * @param name
     * @return
     */
    public R updateCategory(Long id, String name){
        Boolean res = pmsProductAttributeCategoryService.updateCategory(id,name);
        return res?R.success("修改商品属性分类成功"):R.error(500,"修改失败,属性名重复");
    }

    /**
     *获取单个商品属性分类信息
     * @param id
     * @return
     */
    public R getOneById(Long id) {
        PmsProductAttributeCategory pmsProductAttributeCategory = pmsProductAttributeCategoryService.getOneById(id);
        return R.success(pmsProductAttributeCategory);
    }

}
